<template src="./index.html"></template>

<script>
import hospitalService from "../../../../_services/hospital.service";
import doctorAppointment from "../../../../_services/doctorAppointment.service";
import appointmentService from "../../../../_services/appointment.service";
import doctorAppointmentItemService from "../../../../_services/doctorAppointmentItem.service";
import appointmentRecordService from "../../../../_services/appointmentRecord.service";
import wxService from "../../../../_services/wx.service";
import PayService from "../../../../_services/pay.service";
export default {
  name: "TianDingPatientReservationDetailComponent",
  data() {
    return {
      user: {},
      doctor: {},
      params: {
        UserID: 0,
        DoctorID: 0,
        HospitalID: "",
        IsPregnancy: "0",
        RecordHospitalName: "",
        AppointmentID: 0,
        TimeType: 0,
        AppointmentItemID: 0,
        PayType: 0
      },
      showHospital: false,
      hospitalList: [],
      showHospitalLocation: false,
      hospitalLocationList: [],
      appointmentDateList: [],
      showAppointmentItem: false,
      appointmentItemList: [],
      selectDate: {},
      load: false
    };
  },
  mounted() {
    this.doctor = JSON.parse(this.local$.getItem("tdPatientReservationParams"));
    this.user = JSON.parse(this.local$.getItem("tdUser"));
    this.getHospitalListByDoctor();
  },
  methods: {
    //获取医院列表
    getHospitalListByDoctor() {
      this.indicator$.open("正在读取配置...");
      this.hospitalList = [];
      this.hospitalLocationList = [];
      this.hospitalLocationList.push({
        value: "-1",
        label: "无病例"
      });
      this.hospitalLocationList.push({
        value: "-2",
        label: "其他医院"
      });
      let hospitalParams = {};
      hospitalParams.doctorID = this.doctor.DoctorID;
      hospitalService.getHospitalListByDoctor(hospitalParams).then(response => {
        if (response && response.Data.length > 0) {
          response.Data.forEach(element => {
            let item = {
              value: element.HospitalID.toString(),
              label: element.HospitalName
            };
            this.hospitalList.push(item);
            this.hospitalLocationList.push(item);
          });
        }
        this.params.HospitalID = this.hospitalList[0].value;
        this.getAppointmentListByDoctorAndHospital(this.params.HospitalID);
      });
    },
    //获取所选医院预约日期设置
    getAppointmentListByDoctorAndHospital(hospitalID) {
      this.appointmentDateList = [];
      this.load = false;
      let params = {};
      params.doctorID = this.doctor.DoctorID;
      params.hospitalID = hospitalID;
      appointmentService
        .getAppointmentListByDoctorAndHospital(params)
        .then(response => {
          if (response && response.Data.length > 0) {
            response.Data.forEach(element => {
              let item = {};
              item.AppointmentID = element.AppointmentID;
              item.DoctorID = element.DoctorID;
              item.HospitalID = element.HospitalID;
              item.AppointmentDate = element.AppointmentDate.split("T")[0];
              item.AMNumber = element.AMNumber;
              item.PMNumber = element.PMNumber;
              item.NMNumber = element.NMNumber;
              item.AMHad = element.AMHad;
              item.PMHad = element.PMHad;
              item.NMHad = element.NMHad;

              let count = item.AMNumber + item.PMNumber + item.NMNumber;
              let hanCount = item.AMHad + item.PMHad + item.NMHad;

              //当前天 true：已约满、false：可预约
              item.IsAppointmentDate = count === hanCount;

              //当前天 早晨 0可预约、1已约满、2未设置
              item.IsAM = this.getAppointmentStatus(item.AMNumber, item.AMHad);
              //预约按钮是否可用：已约满并且未设置为不可用
              item.IsAMDisabled = item.IsAM === 1 || item.IsAM === 2;

              //当前天 下午 0可预约、1已约满、2未设置
              item.IsPM = this.getAppointmentStatus(item.PMNumber, item.PMHad);
              //预约按钮是否可用：已约满并且未设置为不可用
              item.IsPMDisabled = item.IsPM === 1 || item.IsPM === 2;

              //当前天 晚上 0可预约、1已约满、2未设置
              item.IsNM = this.getAppointmentStatus(item.NMNumber, item.NMHad);
              //预约按钮是否可用：已约满并且未设置为不可用
              item.IsNMDisabled = item.IsNM === 1 || item.IsNM === 2;

              this.appointmentDateList.push(item);
            });
          }
          this.load = true;
          this.indicator$.close();
        });
    },
    //选择医院
    getHospital(event) {
      this.indicator$.open("正在读取配置...");
      this.showHospital = false;
      this.getAppointmentListByDoctorAndHospital(event);
    },
    //选择病例所在地
    getHospitalLocation(event) {
      this.showHospitalLocation = false;
    },
    //点击日历更新日历状态
    update(event) {
      console.log(event);
      this.selectDate = event;
      this.indicator$.open("正在读取配置...");
      this.getAppointmentListByDoctorAndHospital(
        event.appointmentDateItem.HospitalID
      );
    },
    //点击预约按钮，进行项目选择
    btnReservation(event, timeType) {
      if (!this.params.RecordHospitalName) {
        this.toast$(`请选择病例所在地`);
        return;
      }
      this.indicator$.open("正在加载项目...");
      console.log(event);
      event.timePeriod[timeType].flag = true;
      event.timePeriod.filter((v, i, a) => {
        if (i !== timeType) {
          v.flag = false;
        }
      });
      this.showAppointmentItem = true;
      let params = {};
      params.doctorID = this.doctor.DoctorID;
      params.ItemStatus = 0;
      doctorAppointmentItemService
        .getAppointmentItemByDoctorID(params)
        .then(response => {
          if (response && response.Data.length > 0) {
            this.appointmentItemList = response.Data;
          }
          this.indicator$.close();
        });
    },
    //预约下单
    btnOrder(item) {
      this.indicator$.open("请稍后...");

      let params = {
        UserID: this.user.UserID,
        DoctorID: this.doctor.DoctorID,
        HospitalID: parseInt(this.params.HospitalID),
        IsPregnancy: parseInt(this.params.IsPregnancy),
        RecordHospitalName: this.hospitalLocationList.find(
          i => i.value === this.params.RecordHospitalName
        ).label,
        AppointmentID: this.selectDate.appointmentDateItem.AppointmentID,
        TimeType: this.selectDate.timePeriod.findIndex(i => i.flag === true),
        AppointmentItemID: item.AppointmentItemID,
        PayType: 0
      };

      appointmentRecordService.createAppointment(params).then(response => {
        if (response.Data) {
          let orderNumber = response.Data;
          let RequestPayParams = {};
          RequestPayParams.OrderNumber = orderNumber;
          RequestPayParams.UserID = params.UserID;
          RequestPayParams.PayType = params.PayType;
          //发起支付
          PayService.RequestPayAppointmentRecord(RequestPayParams).then(pay => {
            if (pay.Data) {
              let wxParams = JSON.parse(pay.Data.WXJsApiParam);
              wxParams.type = 1;
              wxParams.total = this.utils$.subTransfer(item.ItemPrice);
              wxService.JsSdk(wxParams, this.$router);
            }
            this.indicator$.close();
          });
        }
      });
    },
    getAppointmentStatus(num, han) {
      let status = 0;
      if (num !== han) {
        status = 0;
      }

      if (num === 0) {
        status = 2;
      } else {
        if (num === han) {
          status = 1;
        }
      }
      return status;
    }
  }
};
</script>

